package com.qianfeng.microservice.consumer.controller;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qianfeng.microservice.consumer.feign.AddressFeign;
import com.qianfeng.microservice.consumer.pojo.Address;
import com.qianfeng.microservice.consumer.pojo.Order;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.Random;

/**
 * Created by Jackiechan on 2022/2/25/21:07
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@RestController
@RefreshScope//当监听到配置发生变化的时候会重新加载数据
public class OrderController {

    @Value("${ceshi.name}")
    private String name;



    private AddressFeign addressFeign;

    @Autowired
    public void setAddressFeign(AddressFeign addressFeign) {
        this.addressFeign = addressFeign;
    }


    @GetMapping("/name")
    public String getName() {
        return name;
    }

    @GetMapping("/order/{id}")
    public Order findOrderById(@PathVariable Integer id) {
        Order order = new Order();
        order.setId(id);
        Address address = addressFeign.getById(id);
        order.setAddress(address);
        return order;
    }

    @RequestMapping("/test111")
    public String test1(String name,int age) {
        return addressFeign.test1(name, age);
    }

    @PostMapping("/test222")
    public Address test2(@RequestBody Address address) {
        return addressFeign.test2(address);
    }


    @RequestMapping("/test333")
    @HystrixCommand(commandKey = "nihao",fallbackMethod = "test3jiangji",commandProperties ={@HystrixProperty(name ="execution.isolation.strategy" ,value ="SEMAPHORE" )} )//当当前的方法出现问题的时候使用降级的方法来得到降级的数据
    public String test3() {
        System.err.println(Thread.currentThread().getName());
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "test3";
    }



    @RequestMapping("/test444")
    public String test4() {
        return "test4v2";
    }


    @RequestMapping("/test555")
    @HystrixCommand(fallbackMethod = "test5jiangji")
    public String test5(String name) {
        if (new Random().nextBoolean()) {
            int i = 1 / 0;
        }
        return "test555"+name;
    }

    public String test3jiangji() {
        System.err.println("降级"+Thread.currentThread().getName());
        return "test3jiangji";
    }

    public String test5jiangji(String name) {
        return "test555jiangji"+name;
    }
}
